﻿Imports System.Web.Script.Serialization
Imports System.Web.Script.Services
Public Class oHierarchyLink
    Public Function GetHierarchyDetailByHierarchyID(ByVal dataBaseName As String, ByVal hierarchyID As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oHierarchyLink")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                Dim query As New StringBuilder
                query.Append("SELECT T0.*")
                query.Append(" FROM oHierarchyLink T0 JOIN oHierarchy T1 ON T0.HierarchyID = T1.HierarchyID")
                query.Append("  WHERE T0.HierarchyID = @Param1")
                ds = connect.ObjectGetAll_Query_SAP(query.ToString(), New Object() {hierarchyID})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function CreateHierarchyDetail(ByVal dataBaseName As String, ByVal hierarchyID As String, ByVal isUpdate As Boolean, ByVal lineJson As String) As String
        Dim connect As New Connection()
        Try
            Dim jss As New JavaScriptSerializer()
            Dim lstHierarchyDetail As List(Of HierarchyDetail) = jss.Deserialize(Of List(Of HierarchyDetail))(lineJson)
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            Dim query As New StringBuilder
            If errMsg.Length = 0 AndAlso lstHierarchyDetail IsNot Nothing Then

                If isUpdate = False Then
                    query.Append("INSERT INTO [oHierarchyDetail] ")
                    query.Append(" ([HierarchyID]")
                    query.Append(",[HierarchyDetailName]")
                    query.Append(",[HierarchyFatherID])")
                    query.Append(" VALUES (@HierarchyID{0},@HierarchyDetailName{0},@HierarchyFatherID{0})")
                    Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), hierarchyID, lstHierarchyDetail)
                Else
                    Dim params = Nothing
                    For Each line In lstHierarchyDetail
                        If line.LineStatus = "1" Then
                            query.Append("INSERT INTO [oHierarchyDetail] ")
                            query.Append(" ([HierarchyID]")
                            query.Append(",[HierarchyDetailName]")
                            query.Append(",[HierarchyFatherID])")
                            query.Append(" VALUES (@Param1,@Param2,@Param3)")
                            Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), New Object() {hierarchyID, line.HierarchyDetailName, line.HierarchyFatherID})
                        ElseIf line.LineStatus = "2" Then
                            query.Append("  UPDATE [oHierarchyDetail] SET ")
                            query.Append("  [HierarchyDetailName] = @Param1")
                            query.Append(" ,[HierarchyFatherID] = @Param2")
                            query.Append(" WHERE [HierarchyID] = @Param3 AND HierarchyDetailID =  @Param4")
                            Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), New Object() {line.HierarchyDetailName, line.HierarchyFatherID, hierarchyID, line.HierarchyDetailID})
                        ElseIf line.LineStatus = "3" Then
                            query.Append("  DELETE FROM [oHierarchyDetail]")
                            query.Append("  WHERE [HierarchyID] = @Param1 AND HierarchyDetailID = @Param2")
                            Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), New Object() {hierarchyID, line.HierarchyDetailID})
                        End If

                    Next
                End If
            Else
                Return errMsg
            End If
        Catch ex As Exception
            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Class HierarchyDetail
        Property HierarchyDetailID As Integer
        Property HierarchyID As Integer
        Property HierarchyDetailName As String
        Property HierarchyFatherID As Integer
        Property LineStatus As String
        Public Sub New()
        End Sub
    End Class
End Class
